"""
Case Type   : 功能测试
Case Name   : 双集群在备集群执行failover
Create At   : 2025/03/25
Owner       : @chen
Description :
    1.准备xml配置
    2.构建双集群
    3.在主集群上执行failover
gs_ddr -t failover
    4.构建双集群
    5.在备集群上执行failover并指定log收集位置无权限
    6.在备集群上执行failover并指定log收集位置
    7.恢复环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.合理报错
    6.成功
    7.成功
History     :
"""

import unittest
import os
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.ComThread import ComThread
from testcase.SS_FUNC.gs_ddr.Dual_Cluster_Redution_utils import dcru
from yat.test import Node
from yat.test import macro
logger = Logger()

class Dual_Cluster_Redution_Case0033(unittest.TestCase):
    def setUp(self):
        logger.info(f'--------{os.path.basename(__file__)} start--------')
        self.dcru = dcru()
        self.com = Common()
        self.p_pri_node = Node('P_PrimaryDbUser')
        self.s_pri_root = Node('P_PrimaryRoot')
        self.s_pri_node = Node('S_PrimaryDbUser')
        self.tpcc_node = Node('S_Standby2DbUser')
        self.err_log_path = '/home/gs_ddr_test'
        self.p_xml_path = ''
        self.s_xml_path = ''

    def test_shared_storage(self):
        text = '-----step1:准备xml配置 expect:成功-----'
        logger.info(text)
        self.p_xml_path, self.s_xml_path = self.dcru.xml_generate()
        logger.info(f'主集群xml_path:{self.p_xml_path}')
        logger.info(f'备集群xml_path:{self.s_xml_path}')

        logger.info(f'-----使用root用户创建文件夹-----')
        self.s_pri_root.sh(f'mkdir -p {self.err_log_path}').result()
        failover_param = ['', 
            f'-l {self.err_log_path}/gs_ddr.log', 
            f'-l {macro.DB_MY_PATH}/gs_ddr.log']

        for i in range(3):
            text = f'-----step{(2*i)+2}:构建双集群 expect:成功-----'
            logger.info(text)
            logger.info(f'-----主集群下发命令-----')
            p_start = ComThread(self.dcru.gs_ddr_start, args=(self.p_pri_node,
                'primary', 
                f'-X {self.p_xml_path}', '--disaster_type stream'))
            p_start.setDaemon(True)
            p_start.start()
            s_start = ComThread(self.dcru.gs_ddr_start, args=(self.s_pri_node,
                'disaster_standby', 
                f'-X {self.s_xml_path}', '--disaster_type stream'))
            s_start.setDaemon(True)
            s_start.start()
            res = p_start.get_result()
            logger.info(res)
            self.assertIn('Successfully do dorado disaster recovery start', res)
            res = s_start.get_result()
            logger.info(res)
            self.assertIn('Successfully do dorado disaster recovery start', res)
            if '' == failover_param[i]:
                text = f'-----step{(2*i)+3}:在备集群上执行failover expect:成功-----'
                logger.info(text)
                res = self.dcru.gs_ddr_failover(self.s_pri_node, failover_param[i])
                logger.info(res)
                self.assertIn('Successfully do dorado disaster recovery failover', res)
            elif self.err_log_path in failover_param[i]:
                text = f'-----step{(2*i)+3}:在备集群上执行failover并指定log收集位置无权限' \
                    f' expect:合理报错-----'
                logger.info(text)
                res = self.dcru.gs_ddr_failover(self.s_pri_node, failover_param[i])
                logger.info(res)
                self.assertIn('Permission denied', res)

    def tearDown(self):
        text = '-----step7:恢复环境 expect:成功-----'
        logger.info(text)
        self.p_pri_node.sh(f'rm -rf {self.p_xml_path}').result()
        self.s_pri_node.sh(f'rm -rf {self.s_xml_path}').result()
        self.s_pri_node.sh(f'rm -rf {macro.DB_MY_PATH}/gs_ddr*').result()
        self.s_pri_root.sh(f'rm -rf {self.err_log_path}').result()
        logger.info(f'-----{os.path.basename(__file__)} end-----')
